package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.CmsContentMapper;
import com.originskyseed.platform.cms.model.CmsContent;
import com.originskyseed.platform.cms.service.ICmsContentService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;

/**
 * 内容正文接口实现类
 *
 * @author LiangMingHui
 *
 */
@DubboService
@Component("cmsContentService")
public class CmsContentServiceImpl implements ICmsContentService {

	@Resource
	private CmsContentMapper cmsContentMapper;

	/**
	 * deleteByPrimaryKey
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.cmsContentMapper.deleteByPrimaryKey(id);
	}

	/**
	 * deleteByPrimaryKeys
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.cmsContentMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * insert
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(CmsContent record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.cmsContentMapper.insert(record);
	}

	/**
	 * insertSelective
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(CmsContent record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.cmsContentMapper.insertSelective(record);
	}

	/**
	 * selectByPrimaryKey
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public CmsContent selectByPrimaryKey(String id) throws Exception {
		return this.cmsContentMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<CmsContent> selectByCatalogId(String catalogId) throws Exception {
		return this.cmsContentMapper.selectByCatalogId(catalogId);
	}

	/**
	 * updateByPrimaryKeySelective
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(CmsContent record) throws Exception {
		record.setUpdateTime(new Date());
		return this.cmsContentMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * updateByPrimaryKey
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(CmsContent record) throws Exception {
		record.setUpdateTime(new Date());
		return this.cmsContentMapper.updateByPrimaryKey(record);
	}

	/**
	 * 查询来源ID是否存在引用
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existSource(Map<String, Object> params) throws Exception {
		return this.cmsContentMapper.existSource(params);
	}

	/**
	 * 分页查询回收站列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CmsContent> selectCmsRecycleBinListByPage(Map<String, Object> params, BasePage<CmsContent> page) throws Exception {
		params.put("page", page);
		List<CmsContent> list = this.cmsContentMapper.selectCmsRecycleBinListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量还原
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int RecycleByPrimaryKeys(List<String> ids) throws Exception {
		return this.cmsContentMapper.RecycleByPrimaryKeys(ids);
	}

	/**
	 * 分页查询消息审核代码
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CmsContent> selectAuditListByPage(Map<String, Object> params, BasePage<CmsContent> page) throws Exception {
		params.put("page", page);
		List<CmsContent> list = this.cmsContentMapper.selectAuditListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询信息列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsContent> selectList(Map<String, Object> params) throws Exception {
		return this.cmsContentMapper.selectList(params);
	}

	/**
	 * 根据创建时间和修改时间查询
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsContent> selectCmsByTime(String time, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.cmsContentMapper.selectCmsByTime(time);
	}

	/**
	 * 根据创建时间和修改时间查询
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsContent> selectCmsByTime(String time) throws Exception {
		return this.cmsContentMapper.selectCmsByTime(time);
	}

	/**
	 * 分页查询信息列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CmsContent> selectListByPage(Map<String, Object> params, BasePage<CmsContent> page) throws Exception {
		params.put("page", page);
		List<CmsContent> list = this.cmsContentMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量改变信息状态为删除
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int stateDelByPrimaryKeys(List<String> ids) throws Exception {
		return this.cmsContentMapper.stateDelByPrimaryKeys(ids);
	}

	/**
	 * 置顶
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public void toTop(String id) throws Exception {
		CmsContent cmsContent = this.cmsContentMapper.selectByPrimaryKey(id);
		// 设置该id为置顶
		cmsContent.setIsTop(1);
		cmsContent.setUpdateTime(new Date());

		this.cmsContentMapper.updateNotIsTop(cmsContent);
		this.cmsContentMapper.updateByPrimaryKeySelective(cmsContent);
	}

	/**
	 * 取消置顶
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public void toNotTop(String id) throws Exception {

		// 修改同目录下所有的资讯为不置顶
		CmsContent updateCmsContent = new CmsContent();
		updateCmsContent.setId(id);
		updateCmsContent.setIsTop(0);
		updateCmsContent.setUpdateTime(new Date());

		this.cmsContentMapper.updateByPrimaryKeySelective(updateCmsContent);
	}

	@Override
	public Boolean exitDraft(Map<String, Object> map) throws Exception {
		return this.cmsContentMapper.exitDraft(map);
	}

	/**
	 * 查询所有内容
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsContent> selectAll() throws Exception {
		return this.cmsContentMapper.selectAll();
	}

	@Override
	public BasePage<CmsContent> selectNewsInfoByPage(Map<String, Object> params, BasePage<CmsContent> page) throws Exception {
		params.put("page", page);
		List<CmsContent> list = this.cmsContentMapper.selectListNewInfoByPage(params);
		page.setResults(list);
		return page;
	}
}
